package com.shareach.util;

import java.io.StringWriter;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JsonUtil {
	private static final Log log = LogFactory.getLog(JsonUtil.class);
	private static final ObjectMapper mapper = new ObjectMapper();

	public static String jsonFromObject(Object object) {
		StringWriter writer = new StringWriter();
		try {
			mapper.writeValue(writer, object);
		} catch (RuntimeException e) {
			throw e;
		} catch (Exception e) {
			log.error("Unable to serialize to json: " + object, e);
			return null;
		}
		return writer.toString();
	}


	@SuppressWarnings("deprecation")
	public static <T> T objectFromJson(String json, Class<T> klass) {
		T object;
		try {//设置输入时忽略JSON字符串中存在而Java对象实际没有的属性  
			mapper.getDeserializationConfig().set(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			object = mapper.readValue(json, klass);
		} catch (RuntimeException e) {
			log.error("Runtime exception during deserializing "
					+ klass.getSimpleName() + " from "
					+ StringUtils.abbreviate(json, 80));
			throw e;
		} catch (Exception e) {
			log.error("Exception during deserializing " + klass.getSimpleName() + " from " + StringUtils.abbreviate(json, 80));
			return null;
		}
		return object;
	}

	@SuppressWarnings("deprecation")
	public static <T> T objectFromJson(String json,TypeReference<T> klass) {
		T object;
		try {//设置输入时忽略JSON字符串中存在而Java对象实际没有的属性  
			mapper.getDeserializationConfig().set(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			object = mapper.readValue(json, klass);
		} catch (RuntimeException e) {
			log.error("Runtime exception during deserializing from "
					+ StringUtils.abbreviate(json, 80));
			throw e;
		} catch (Exception e) {
			log.error("Exception during deserializing from " + StringUtils.abbreviate(json, 80));
			return null;
		}
		return object;
	}
	
	/**
	 * mapper.readValue(jsonInput, new TypeReference<List<MyClass>>(){});
	 * @param json
	 * @param klass
	 * @return
	 */
	public static  <T> T[]  listFromJson(String json,Class<T[]> klass) {
		T[] logs;
		try {
			logs = mapper.readValue(json,klass);
		} catch (RuntimeException e) {
			log.error("Runtime exception during deserializing ");
			throw e;
		} catch (Exception e) {
			log.error("Exception during deserializing "
					+ " from " + StringUtils.abbreviate(json, 80));
			return null;
		}
		return logs;
	}
	/**
	 * mapper.readValue(jsonInput, new TypeReference<List<MyClass>>(){});
	 * @param json
	 * @param klass
	 * @return
	 */
	public static  <T> T[]  listFromJson(String json,TypeReference<T> klass) {
		T[] logs;
		try {
			logs = mapper.readValue(json,klass);
		} catch (RuntimeException e) {
			log.error("Runtime exception during deserializing ");
			throw e;
		} catch (Exception e) {
			log.error("Exception during deserializing "
					+ " from " + StringUtils.abbreviate(json, 80));
			return null;
		}
		return logs;
	}
	
}
